<template>
  <div>
    <template>
      <!--      查询-->
      <el-form ref="formveh" :model="formveh" label-width="80px">
        <el-form-item>
          <el-input v-model="formveh.likeStr" style="width:300px;margin-left: 500px;margin-top: 20px" placeholder="请输入车牌号、姓名、证件号"></el-input>
          <el-button type="primary" @click="VehList">查询</el-button>
        </el-form-item>
      </el-form>

      <el-button size="mini" type="danger" style="margin-top: -100px" @click="handleDelete">删除</el-button>

      <!--      列表-->
      <el-table  :data="veh" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="编号" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.vehicleInformationId }}</span>
          </template>
        </el-table-column>

        <el-table-column label="车牌号" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.vehicleInformationName }}</span>
          </template>
        </el-table-column>

        <el-table-column label="车辆图片" width="180">
          <template slot-scope="scope">
            <img :src="scope.row.vehicleInformationPic" alt="暂无" width="100px" height="50px">
          </template>
        </el-table-column>

        <el-table-column label="小区" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column label="楼栋号" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.buildingNumber }}号</span>
          </template>
        </el-table-column>

        <el-table-column label="单元(号)" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.elementNumber }}单元</span>
          </template>
        </el-table-column>

        <el-table-column label="房间号" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.roomNumber }}</span>
          </template>
        </el-table-column>

        <el-table-column label="证件号" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.idCard }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row.vehicleInformationId)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--      弹窗添加车辆-->
      <el-button type="text" @click="addcars">添加车辆</el-button>
      <el-dialog :title=title :visible.sync="dialogFormVisible">
        <el-form :model="form"  :rules="rules" ref="ruleForm">
          <el-form-item label="车牌号" :label-width="formLabelWidth" prop="vehicleInformationName">
            <el-input v-model="form.vehicleInformationName" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="图片" :label-width="formLabelWidth" >
            <el-upload
              action="/dev-api/system/veh/img"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :on-success="onSuccess"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="30px" height="40px" :src="dialogImageUrl" alt="">
            </el-dialog>
            <!-- 控制图片大小 -->
            <img :src="form.vehicleInformationPic" alt="暂无" class="image-preview" />
          </el-form-item>

          <el-form :model="form"  :rules="rules" ref="ruleForm">
            <el-form-item label="关联车主" :label-width="formLabelWidth" prop="uid">
              <el-input v-model="form.uid" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>


        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="evhAdd">确 定</el-button>
        </div>
      </el-dialog>
      <div class="fen">
        <span class="demonstration">分页</span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="formveh.pageNum"
          :page-sizes="[20, 50, 80, 100]"
          :page-size="formveh.pageSize"
          layout="sizes, prev, pager, next"
          :total="total">
        </el-pagination>
      </div>

    </template>

  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
//例如：import 《组件名称》 from '《组件路径》,
import {add, list} from "@/api/system/vehicle";
import {del} from "@/api/system/vehicle";
import {echo} from "@/api/system/vehicle";
import {MessageBox} from "element-ui";

export default {
  //import引入的组件需要注入到对象中才能使用"
  components: {},
  props: {},
  data() {
    return {
      //列表
      veh:[],
      total:0,
      //查询条件
      formveh:{
        pageNum:1,
        pageSize:20,
        likeStr:'',
      },

      //图片
      dialogImageUrl: '',
      dialogVisible: false,
      //选中
      multipleSelection: [],
      //弹窗
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: '120px',
      title: '添加车辆',
      form:{
        vehicleInformationPic:'',
        vehicleInformationName:'',
        uid:'',
      },
      //表单效验
      ruleForm: {
        vehicleInformationName:'',
        vehicleInformationPic:'',
        uid:'',
      },
      rules:{
        vehicleInformationName: [
          { required: true, message: '请输入车牌号', trigger: 'blur' },
        ],
        uid:[
          { required: true, message: '请输入关联车主', trigger: 'blur' },
        ],
      }

    };

    //这里存放数据"
  },
  //计算属性 类似于data概念",
  computed: {},
  //监控data中的数据变化",
  watch: {},
  //方法集合",
  methods: {

    //批量删除
    handleDelete(){
      if (this.multipleSelection.length===0){
        this.$message.warning("请选择一个");
        return;
      }
      this.$confirm("确认删除吗?", "提示", {
        confirmButtonText:'确认',
        cancelButtonText:'取消',
        type: "warning",
      }).then(() => {
        let vehicleInformationId = []
        this.multipleSelection.forEach(item => {
          vehicleInformationId.push(item.vehicleInformationId)
        })
        del(vehicleInformationId).then(
          res => {
            this.$message.success("删除成功");
            this.VehList()
          }
        )
      })
    },

    //回显
    handleEdit(vehicleInformationId){
      this.title='回显车辆'
      this.dialogFormVisible = true;
      echo(vehicleInformationId).then(
        res => {
          this.form=res.data
        }
      )
    },

    //选中
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //添加
    evhAdd(){
      MessageBox.confirm('确认添加吗?','添加',{
        confirmButtonText:'确认',
        cancelButtonText:'取消',
        type: "warning",
      }).then(() => {
        add(this.form).then(
          res=>{
            this.$message.success("添加成功");
            this.dialogFormVisible = false;
            this.VehList()
          }
        )
      })
    },
    //列表
    VehList(){
      list(this.formveh).then(
        res=>{
          this.veh=res.data.list
          this.total=res.data.total
        }
      )
    },
    //图片添加
    handleRemove(file, fileList) {
      this.form.vehicleInformationPic=''

      for (let fileListKey in fileList) {
        this.form.vehicleInformationPic.push(fileList[fileListKey].response.data)
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //上传成功钩子函数
    onSuccess(response, file, fileList){
      this.form.vehicleInformationPic=response.data
    },
    //分页
    handleSizeChange(val) {
      this.page.pageNum=val
      console.log(`每页 ${val} 条`);
      this.VehList()
    },
    handleCurrentChange(val) {
      this.page.pageSize=val
      console.log(`当前页: ${val}`);
      this.VehList()
    },
    addcars(){
      this.dialogFormVisible = true
      //清空缓存
      this.form={
        vehicleInformationPic:'',
        vehicleInformationName:'',
        userId:''
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）",
  created() {
    this.VehList()
  },
  //生命周期 - 挂载完成（可以访问DOM元素）",
  mounted() {
  },
  beforeCreate() {
  }, //生命周期 - 创建之前",
  beforeMount() {
  }, //生命周期 - 挂载之前",
  beforeUpdate() {
  }, //生命周期 - 更新之前",
  updated() {
  }, //生命周期 - 更新之后",
  beforeDestroy() {
  }, //生命周期 - 销毁之前",
  destroyed() {
  }, //生命周期 - 销毁完成",
  activated() {
  } //如果页面有keep-alive缓存功能，这个函数会触发",
};
</script>
<style scoped>
.image-preview{
  max-width: 150px;/* 最大宽度*/
  max-height: 4500px;/* 最大高度 */
  object-fit: contain;/* 保持图片比例*/
}
</style>
